
package com.hsc.www.rawTest.事务;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;

public class 解决办法B {
    public static void main(String[] args) throws Exception {
        //1、创建ConnectionFactory
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setVirtualHost("my_vhost");
        connectionFactory.setPort(5672);

        //2、获取 connection
        Connection connection = connectionFactory.newConnection();

        //3、通过connection创建一个新的Channel
        Channel channel = connection.createChannel();

        //4、指定我们的消息投递模式: 消息的确认模式
        channel.confirmSelect();
        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.err.println("-------no ack!-----------deliveryTag=" + deliveryTag + "  multiple=" + multiple);
            }

            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.err.println("-------ack!-----------deliveryTag=" + deliveryTag + "  multiple=" + multiple);
            }
        });

        String QUEUE_NAME = "tx_queue";
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        for (int i = 0; i < 10; i++) {
            channel.basicPublish("", QUEUE_NAME, null, "消息".getBytes());
        }


    }
}
